package com.atguigu.chapter07;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.GlobalWindows;
import org.apache.flink.streaming.api.windowing.triggers.Trigger;
import org.apache.flink.streaming.api.windowing.triggers.TriggerResult;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.util.Collector;

import java.util.ArrayList;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/7/19 10:10
 */
public class Flink05_Widow_Global {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        env
            .socketTextStream("hadoop162", 9999)
            .flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
                @Override
                public void flatMap(String value,
                                    Collector<Tuple2<String, Long>> out) throws Exception {
                    for (String word : value.split(" ")) {
                        out.collect(Tuple2.of(word, 1L));
                        
                    }
                }
            })
            .keyBy(t -> t.f0)
            .window(GlobalWindows.create())
            .trigger(new Trigger<Tuple2<String, Long>, GlobalWindow>() { // 基于个数的个数: 窗口元素的个数到达3的时候触发
                // 每来一个元素, 触发一次这个方法
                int count = 0;
                
                @Override
                public TriggerResult onElement(Tuple2<String, Long> element,
                                               long timestamp,
                                               GlobalWindow window,
                                               TriggerContext ctx) throws Exception {
                    System.out.println("Flink05_Widow_Global.onElement");;
                    count++;
                    if (count % 3 == 0) {
                        return TriggerResult.FIRE_AND_PURGE;
                    } else {
                        
                        return TriggerResult.CONTINUE;
                    }
                }
                
                // 当基于处理时间的窗口, 会触发这个函数
                @Override
                public TriggerResult onProcessingTime(long time,
                                                      GlobalWindow window,
                                                      TriggerContext ctx) throws Exception {
                    System.out.println("Flink05_Widow_Global.onProcessingTime");
                    return null;
                }
                
                // 当基于实践时间的窗口, 会触发这个函数
                @Override
                public TriggerResult onEventTime(long time,
                                                 GlobalWindow window,
                                                 TriggerContext ctx) throws Exception {
                    System.out.println("Flink05_Widow_Global.onEventTime");
                    return null;
                }
                
                // 清除一些状态
                @Override
                public void clear(GlobalWindow window, TriggerContext ctx) throws Exception {
                    System.out.println("Flink05_Widow_Global.clear");
                }
            })
            .process(new ProcessWindowFunction<Tuple2<String, Long>, String, String, GlobalWindow>() {
                @Override
                public void process(String key,
                                    Context ctx,
                                    Iterable<Tuple2<String, Long>> elements,
                                    Collector<String> out) throws Exception {
                    ArrayList<String> words = new ArrayList<>();
                    for (Tuple2<String, Long> ele : elements) {
                        words.add(ele.f0);
                    }
                    
                    out.collect("key=" + key + ", window=" + ctx.window() + ",words=" + words);
                    
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
